<?php
class Province_model extends Model{

	var $_datagrid_name = 'dt1';	
	
    function Province_model(){
        parent::Model();       
    }
    function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('realestate_province');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,name,sortorder');
		$this->db->order_by('sortorder', 'asc');
    	$query = $this->db->get('realestate_province');
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $query->result_rowlist();
    	return $datagrid;    	
    }
    function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$query = $this->db->get_where('realestate_province',array('id'=>$id));
    		if($query->num_rows()){    					
    			$this->db->delete('realestate_province',array('id'=>$id));
    		}
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
	    		$query = $this->db->get_where('realestate_province',array('id'=>$id[$i]));
	    		if($query->num_rows()){    					
	    			$this->db->delete('realestate_province',array('id'=>$id[$i]));
	    		}
    		}
    	}
    }
    function getnewprofile(){
		$res->username = $this->input->post('txt_username');
		$res->firstname = $this->input->post('txt_firstname');
		$res->lastname = $this->input->post('txt_lastname');
		$res->phone = $this->input->post('txt_phone');
		$res->email = $this->input->post('txt_email');
		$res->alertmsg = empty($res->username)?"":lang('usermanagement_msg_invalidusername');
		return $res;
	}
	function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
			$res = $this->db->get_where('realestate_province',array('name'=>$this->input->post('txt_province')));
			if($res->num_rows) return FALSE;
    		$this->db->insert('realestate_province',array('name'=>$this->input->post('txt_province'),										
											'sortorder'=>$this->input->post('txt_sort')));
    		redirect('list_province','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$this->db->where('id',$this->input->post('hidden_id'));			
			if($this->input->post('txt_name')!='' && $this->input->post('txt_sort')!=''){
				$this->db->update('realestate_province',array('name'=>$this->input->post('txt_name'),
															  'sortorder'=>$this->input->post('txt_sort')));
			}
			redirect('list_province','location');
    	}
    }
   	function getuserprofile($id){
    	$query = $this->db->get_where('realestate_province',array('id'=>$id));
    	if($query->num_rows){
    		return $query->row();
    	}
    	else return FALSE;
    }
   function save(){
		if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_2');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('realestate_province',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}
		
    }
}

/* End of file yourprofile_model.php */
/* Location: ./system/application/admin/controllers/yourprofile_model.php */